/*
============================================================================
 Name        : Titlebar.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CTitlebar implementation
============================================================================
*/

#include "Titlebar.h"
#include "Global.h"
#include "ImageTool.h"
#include "eStockDef.h"
#include <eStock_L2HY.rsg>
#include "BaseCommon.h"
#include "block.h"
#include "eStockAppUi.h"
#include "Tabbar.h"

CTitlebar::CTitlebar():
iFoucusEnabled(EFalse)
{
}

CTitlebar::~CTitlebar()
{
	if(iLogo != NULL)
		delete iLogo, iLogo = NULL;
	if(iLogomask != NULL)
		delete iLogomask, iLogomask = NULL;
	if (iSearchBitmap)
		delete iSearchBitmap, iSearchBitmap = NULL;
	if (iSearchBitmapmask)
        delete iSearchBitmapmask, iSearchBitmapmask = NULL;
    if (iSearchFocusBitmap)
        delete iSearchFocusBitmap, iSearchFocusBitmap = NULL;
    if (iSearchFocusBitmapmask)
        delete iSearchFocusBitmapmask, iSearchFocusBitmapmask = NULL;
    if (iLeftarrowBitmap)
        delete iLeftarrowBitmap, iLeftarrowBitmap = NULL;
	if (iLeftarrowBitmapmask)
		delete iLeftarrowBitmapmask, iLeftarrowBitmapmask = NULL;
	if (iRightarrowBitmap)
		delete iRightarrowBitmap, iRightarrowBitmap = NULL;
	if (iRightarrowBitmapmask)
		delete iRightarrowBitmapmask, iRightarrowBitmapmask = NULL;
	
	CEikonEnv::Static()->ScreenDevice()->ReleaseFont(iFont);
}

CTitlebar* CTitlebar::NewLC()
{
	CTitlebar* self = new (ELeave)CTitlebar();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CTitlebar* CTitlebar::NewL()
{
	CTitlebar* self=CTitlebar::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CTitlebar::ConstructL()
{
	iLogo = new (ELeave) CFbsBitmap;
	iLogomask = new (ELeave) CFbsBitmap;
	CImageTool::CreateFbsBmpFromPng(_L("logo.png"), iLogo, iLogomask);

	iSearchBitmap = new (ELeave) CFbsBitmap;
	iSearchBitmapmask = new (ELeave) CFbsBitmap;
	if (CGlobal::Static()->g_nMobileOS == 5)
	    CImageTool::CreateFbsBmpFromPng(_L("stock_search_big.png"), iSearchBitmap, iSearchBitmapmask);
	else
	    CImageTool::CreateFbsBmpFromPng(_L("stock_search_small.png"), iSearchBitmap, iSearchBitmapmask);

	iSearchFocusBitmap = new (ELeave) CFbsBitmap;
	iSearchFocusBitmapmask = new (ELeave) CFbsBitmap;
	if (CGlobal::Static()->g_nMobileOS == 5)
	    CImageTool::CreateFbsBmpFromPng(_L("stock_search_focus_big.png"), iSearchFocusBitmap, iSearchFocusBitmapmask);
	else
	    CImageTool::CreateFbsBmpFromPng(_L("stock_search_focus_small.png"), iSearchFocusBitmap, iSearchFocusBitmapmask);
	
	iLeftarrowBitmap = new (ELeave) CFbsBitmap;
	iLeftarrowBitmapmask = new (ELeave) CFbsBitmap;
	CImageTool::CreateFbsBmpFromPng(_L("switchleftarrow.png"), iLeftarrowBitmap, iLeftarrowBitmapmask);
	
	iRightarrowBitmap = new (ELeave) CFbsBitmap;
	iRightarrowBitmapmask = new (ELeave) CFbsBitmap;
	CImageTool::CreateFbsBmpFromPng(_L("switchrightarrow.png"), iRightarrowBitmap, iRightarrowBitmapmask);

	CGlobal::GetDesFromID(iTitleText, LOC_BUF_MENU_TITLE);
}

void CTitlebar::Draw(CFbsBitGc& g)
{
	g.SetBrushStyle(CGraphicsContext::ENullBrush);
	g.SetPenStyle(CGraphicsContext::ESolidPen);
	CGlobal::DrawGradientRect(g, m_rect, KRgbBarMian, KRgbBarMian, KRgbBarMian/*KRgbBarLight, KRgbBarLight*/, KRgbBarMian);
 
	// logo
	TRect logoRect(m_rect.iTl, TSize(m_rect.Height(), m_rect.Height()));
	logoRect.Shrink(1, 1);
	g.DrawBitmapMasked(logoRect, iLogo, TRect(TPoint(0,0),iLogo->SizeInPixels()), iLogomask, ETrue);

	TRect rect(TPoint(m_rect.iTl.iX + m_rect.Height() + 2, m_rect.iTl.iY), m_rect.Size());
	// title
	g.UseFont(iFont);
	
	g.SetPenColor(KRgbTitleAndAbaText);
	TInt baseLine = m_rect.Height() / 2 + iFont->AscentInPixels() - iFont->HeightInPixels() / 2;
	if (iFoucusEnabled)
	{	
		TInt spaceX = 6;
		iLeftFocusRect.SetRect(TPoint(rect.iTl.iX + spaceX, rect.Center().iY - iLeftarrowBitmap->SizeInPixels().iHeight / 2), iLeftarrowBitmap->SizeInPixels());
		g.DrawBitmapMasked(iLeftFocusRect, iLeftarrowBitmap, TRect(TPoint(0, 0), iLeftFocusRect.Size()), iLeftarrowBitmapmask, EFalse);
		iLeftFocusRect.SetRect(TPoint(rect.iTl.iX + spaceX, rect.iTl.iY), TPoint(rect.iTl.iX + spaceX + iLeftarrowBitmap->SizeInPixels().iWidth + 10, rect.iBr.iY));
		spaceX += iLeftFocusRect.Width();
		
		TInt titleWidth = iFont->TextWidthInPixels(iTitleText);
		TInt titleWidth2 = iFont->TextWidthInPixels(iTitleText2);
		if(iFocusOnGoodName)
		{
			TRect focusRect(TPoint(rect.iTl.iX + spaceX, m_rect.iTl.iY), 
			    TSize(titleWidth + (iTitleText2.Length() > 0 ? (titleWidth2 + 5) : 0), m_rect.Height()));
			CGlobal::DrawGradientRect(g, focusRect, TRgb(0xFE, 0xC9, 0x60), TRgb(0xFC, 0xB2, 0x32), 
					TRgb(0xF6, 0x85, 0x00), TRgb(0xF1, 0x72, 0x00));
		}
		
		if (iTitleText2.Length() > 0)
		{
		    g.SetPenColor(KRgbTitleAndAbaText);
			g.DrawText(iTitleText, TRect(TPoint(rect.iTl.iX + spaceX, rect.iTl.iY), TSize(titleWidth, rect.Height())), baseLine);
			spaceX += (titleWidth + 5);
			g.SetPenColor(CGlobal::Static()->g_rgbNameCode);
			g.DrawText(iTitleText2, TRect(TPoint(rect.iTl.iX + spaceX, rect.iTl.iY), TSize(titleWidth2, rect.Height())), baseLine);
			spaceX += (titleWidth2 + 10);
		}
		else
		{
		    g.SetPenColor(CGlobal::Static()->g_rgbNameCode);
			g.DrawText(iTitleText, TRect(TPoint(rect.iTl.iX + spaceX, rect.iTl.iY), TSize(titleWidth, rect.Height())), baseLine);
			spaceX += (titleWidth + 10);
		}
		
		iRightFocusRect.SetRect(TPoint(rect.iTl.iX + spaceX, rect.Center().iY - iRightarrowBitmap->SizeInPixels().iHeight / 2), 
			iRightarrowBitmap->SizeInPixels());
		g.DrawBitmapMasked(iRightFocusRect, iRightarrowBitmap, TRect(TPoint(0, 0), iRightFocusRect.Size()), iRightarrowBitmapmask, EFalse);
		iRightFocusRect.SetRect(TPoint(rect.iTl.iX + spaceX - 10, rect.iTl.iY), TPoint(rect.iTl.iX + spaceX + iLeftarrowBitmap->SizeInPixels().iWidth, rect.iBr.iY));
	} 
	else
		g.DrawText(iTitleText, rect, baseLine);
	
	g.DiscardFont();

	//search
	TSize bitmapSize(iSearchBitmap->SizeInPixels());
    TPoint pos(m_rect.iBr.iX - bitmapSize.iWidth - 1, m_rect.iTl.iY);
    iSearchRect.SetRect(pos, bitmapSize);
    if (iFocusOnSearch)
        g.BitBltMasked(pos, iSearchFocusBitmap, TRect(TPoint(0, 0), bitmapSize), iSearchFocusBitmapmask, EFalse);
    else
        g.BitBltMasked(pos, iSearchBitmap, TRect(TPoint(0, 0), bitmapSize), iSearchBitmapmask, EFalse);
	g.SetBrushStyle(CGraphicsContext::ESolidBrush);
}

void CTitlebar::SetTitleText(const TDesC& aText)
{
	iTitleText.Copy(aText);
}

void CTitlebar::SetTitleText2(const TDesC& aText)
{
    iTitleText2.Copy(aText);
}

TBool CTitlebar::OnKeyDown(const TInt& keycode)
{
	if (keycode == CGlobal::Static()->g_nKeyLeft)
    {
        if (iObserver != NULL && iFocusOnGoodName)
            iObserver->HandleTitlebarCommand(Left);
        return ETrue;
    } 
	else if (keycode == CGlobal::Static()->g_nKeyRight)
	{
		if (iObserver != NULL && iFocusOnGoodName)
			iObserver->HandleTitlebarCommand(Right);
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeyUp)
	{
	    if (iFoucusEnabled)
	    {
	        if (iFocusOnSearch)
	        {
	            iFocusOnSearch = EFalse;
	            iFocusOnGoodName = ETrue;
	        }
	        else if (iFocusOnGoodName)
	        {
	            SetFocus(EFalse);
	            BASECOMMON->iToolbar->SetFocus(ETrue);
	        }
	    }
	    else
	    {
	        if (iFocusOnSearch)
	        {
	            SetFocus(EFalse);
	            BASECOMMON->iToolbar->SetFocus(ETrue);
	        }
	    }
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeyDown)
	{
	    if (iFocusOnGoodName)
	    {
	        iFocusOnGoodName = EFalse;
	        iFocusOnSearch = ETrue;
	    }
	    else
	    {
	        SetFocus(EFalse);
	        BASECOMMON->iTabbar->SetFocus(ETrue, CGlobal::Static()->g_nKeyDown);
	    }
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeySelect || keycode == CGlobal::Static()->g_nKeyOK)
	{
	    if (iObserver != NULL && iFocusOnSearch)
	    {
	        SetFocus(EFalse);
	        iObserver->HandleTitlebarCommand(Search);
	    }
	    return ETrue;
	}

	return EFalse;
}

void CTitlebar::SetRect(const TInt& left,const TInt& top,const TInt& right, const TInt& bottom)
{
	CControl::SetRect(left,top,right,bottom);
	if (iFont == NULL)
	{
		CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();
		TFontSpec myFontSpec = CEikonEnv::Static()->NormalFont()->FontSpecInTwips();
		myFontSpec.iHeight = screenDevice->VerticalPixelsToTwips(m_rect.Height() - 6);
		screenDevice->GetNearestFontInTwips(iFont,myFontSpec);
	}
}

TBool CTitlebar::OnMouseDown(const TPoint& /*aPt*/)
{
	return EFalse;
}

TBool CTitlebar::OnMouseUp(const TPoint& aPt)
{
	if (iLeftFocusRect.Contains(aPt))
	{
		if (iObserver != NULL)
			iObserver->HandleTitlebarCommand(Left);
		return ETrue;
	}
	else if (iRightFocusRect.Contains(aPt))
	{
		if (iObserver != NULL)
			iObserver->HandleTitlebarCommand(Right);
		return ETrue;
	}
	else if (iSearchRect.Contains(aPt))
	{
		BASECOMMON->m_kb->m_bInputGoodCode = ETrue;
		BASECOMMON->m_kb->InitKeys();
		BASECOMMON->m_kb->m_InputLength = 6;
		BASECOMMON->m_kb->m_strInput.Zero();
		BASECOMMON->m_kb->SetInputType(CKeyBoard::NUMERICANDLETTER);
		CBaseCommon::Static()->m_bShowKB = ETrue;
		return ETrue;
	}
	else if (m_rect.Contains(aPt))
	{
		return ETrue;
	}
	return EFalse;
}	

void CTitlebar::FocusChanged(TBool aFocus, const TInt keyCode)
{
	if (aFocus)
	{
        if (keyCode == CGlobal::Static()->g_nKeyDown)
        {
            if (iFoucusEnabled)
                iFocusOnGoodName = ETrue;
            else
                iFocusOnSearch = ETrue;
        }
        else if (keyCode == CGlobal::Static()->g_nKeyUp)
                iFocusOnSearch = ETrue;
	}
	else
	{
	    iFocusOnGoodName = EFalse;
	    iFocusOnSearch = EFalse;
	}
}

void CTitlebar::FocusEnabled(TBool aFocus)
{
	iFoucusEnabled = aFocus;
	if (!iFoucusEnabled)
		CGlobal::GetDesFromID(iTitleText, LOC_BUF_MENU_TITLE);
}
